package com.example.monthmoni.exception;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 宋伟宁
 * @Date 2023/11/28
 * @Version 1.0
 **/
//@RestControllerAdvice
public class GlobaException {

     @ExceptionHandler(RuntimeException.class)
     public void handleException(RuntimeException e, HttpServletResponse response) throws IOException {
         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
         response.setContentType("application/json;charset=utf-8");
         PrintWriter out = response.getWriter();
         String message = e.getMessage();
         out.write("{\"code\":\"201\",\"msg\":+message+}");
         out.flush();
         out.close();
     }
}
